package com.wanwen.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanwen.dto.StudentSearCon;
import com.wanwen.entity.Asker;
import com.wanwen.entity.AutoConfig;
import com.wanwen.entity.Student;
import com.wanwen.mapper.AskerMapper;
import com.wanwen.mapper.AutoConfigMapper;
import com.wanwen.mapper.StudentMapper;
import com.wanwen.utils.Result;
import com.wanwen.utils.ResultData;
import com.wanwen.utils.StringUtils;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
	private StudentMapper studentMapper;
    @Autowired
    private AskerMapper askermapper;
    @Autowired
    private AutoConfigMapper autoConfigMapper;
    /**
	 * 多条件分页查询
	 * 
	 * @param usc 条件对象
	 * @return
	 */
	@RequestMapping("/getAllbyLimit")
	public Object showAllByCon(StudentSearCon ssc) {
		// 封装条件查询构造器
		QueryWrapper<Student> wrapper = new QueryWrapper<Student>();
		wrapper.like(StringUtils.isnotNull(ssc.getUname()),"uname", ssc.getUname());
		wrapper.eq(StringUtils.isnotNull(ssc.getPhone()),"phone", ssc.getPhone());
		wrapper.eq(StringUtils.isnotNullObj(ssc.getUid()),"uid", ssc.getUid());
		wrapper.eq(StringUtils.isnotNullObj(ssc.getUidzx()),"uidzx", ssc.getUidzx());
		wrapper.isNull(StringUtils.isnotNullObj(ssc.getIsfp()) &&ssc.getIsfp()==0, "uid");
		wrapper.like(StringUtils.isnotNullObj(ssc.getIsfp()) &&ssc.getIsfp()==1,"uid","");
		wrapper.eq(StringUtils.isnotNull(ssc.getIspay()),"is_pay", ssc.getIspay());
		wrapper.eq(StringUtils.isnotNull(ssc.getIsreport()),"is_report", ssc.getIsreport());
		wrapper.eq(StringUtils.isnotNull(ssc.getIsvisit()),"is_visit", ssc.getIsvisit());
		wrapper.eq(StringUtils.isnotNull(ssc.getQq()),"qq", ssc.getQq());
		wrapper.ge(StringUtils.isnotNullObj(ssc.getCreatsTime()),"creat_time", ssc.getCreatsTime());
		wrapper.le(StringUtils.isnotNullObj(ssc.getCreateTime()),"creat_time", ssc.getCreateTime());
		// 封装分页对象
		Page<Student> page = new Page<Student>(ssc.getPage(), ssc.getLimit());
		Page<Student> userPage = studentMapper.selectPage(page, wrapper);
		return new ResultData(0, "", userPage.getRecords(), userPage.getTotal());
	}

/**
 * /student/add
 * 添加学生
 * @param student
 * @return rs
 * */
@RequestMapping("/add")
public Object add(Student student) {
	AutoConfig autoConfig = autoConfigMapper.selectOne(null);
	if(autoConfig.getIsAuto().equals("开启")) {
		QueryWrapper<Asker> askerWrapper = new QueryWrapper<Asker>()
				.eq("remark", "已签到").or().eq("remark", "未分配").or().eq("remark", "已分配");
		List<Asker> askerList = askermapper.selectList(askerWrapper );
		if(askerList.size()!=0) {
			//获取当天签到的权重最大的未分配的咨询师信息
			return setChangeState(student);
		}
	}
	Result rs = new Result("", "", studentMapper.insert(student)==1?true:false);
	return rs;
}

/**
 * /student/update
 * 分配学生
 * @param student
 * @return rs
 * */
@RequestMapping("/fenpei")
public Object fenpei(Student student) {
	UpdateWrapper<Student> updateWrapper = new UpdateWrapper<Student>()
			.set("uname", student.getUname())
			.set("uid", student.getUid())
			.eq("id", student.getId());
	Result rs = new Result(student.getId()+"", "", studentMapper.update(student, updateWrapper)==1?true:false);
	return rs;
}
/**
 * /student/update
 * 修改学生
 * @param student
 * @return rs
 * */
@RequestMapping("/update")
public Object update(Student student) {
	UpdateWrapper<Student> updateWrapper = new UpdateWrapper<Student>()
			.set("uname", student.getUname())
			.set("sex", student.getSex())
			.set("address", student.getAddress())
			.set("qq", student.getQq())
			.set("phone", student.getPhone())
			.eq("id", student.getId());
	Result rs = new Result(student.getId()+"", "", studentMapper.update(student, updateWrapper)==1?true:false);
	return rs;
}
/**
 * /student/del
 * 删除学生
 * @param student
 * @return rs
 * */
@RequestMapping("/del")
public Object del(Student student) {
	Result rs = new Result(student.getId()+"", "", studentMapper.deleteById(student)==1?true:false);
	return rs;
}
/**
 * /users/dels 批量删除用户
 * 
 * @param ids
 * @return rs
 */
@RequestMapping("/dels")
public Object del(String ids) {
	System.err.println(ids);
	String[] idsStr=ids.split(",");
	int r = 0;
	for(String s:idsStr) {
		System.err.println(s);
		Student student = new Student();
		student.setId(Integer.parseInt(s));
		r += studentMapper.deleteById(student);
	}
	Result rs = new Result("删除了"+r+"名学生", "", r != 0 ? true : false);
	return rs;
}

public Result setChangeState(Student stu) {
	//找当天已签到的未分配的权重最大的咨询师信息
	QueryWrapper<Asker> wrapper=new QueryWrapper<Asker>();
	wrapper.eq("remark", "已签到").or().eq("remark", "未分配")
	.orderByAsc("weight");
	List<Asker> allAsList=askermapper.selectList(wrapper);
	if(allAsList.size()>0) { // 有, asker ,把该咨询师人员的状态改为已分配
		Asker asker= allAsList.get(0);
		stu.setUid(asker.getUid());
		//把该咨询师人员的状态改为已分配
		asker.setRemark("已分配");
		QueryWrapper<Asker> wrapper2=new QueryWrapper<Asker>();
		wrapper2.eq("id", asker.getId());
		askermapper.update(asker, wrapper2);
		return new Result("", "", studentMapper.insert(stu)==1?true:false);
	}else {//没有,就更新当天签到的所有咨询师人员的状态为未分配
		QueryWrapper<Asker> wrapper3=new QueryWrapper<Asker>();
		wrapper3.eq("remark", "已分配");
		List<Asker> allAskerList=askermapper.selectList(wrapper3);
		  //获取当天签到的权重最大的未分配的咨询师信息
		for(Asker ars:allAskerList) {
			ars.setRemark("未分配");
			QueryWrapper<Asker> wrapper4=new QueryWrapper<Asker>();
			wrapper4.eq("id", ars.getId());
			askermapper.update(ars,wrapper4);
		}
		return 	setChangeState(stu);//递归调用;
   }
}
}
